from strategy.StrategyTemplate import StrategyTemplate
from utils.jsl_data_map import default_columns
from strategy import strategy_filter
from strategy.xm_strategy.xm_strategy_common import reedem_column, reedem_combine_from_alive_column
from utils.pd_show_utils import df_table
from utils.db import DbHandler
from data_tool.table_data_tools import up_price
import pandas as pd

class BondReedem(StrategyTemplate):
    """
    强赎博弈策略:
        基于下修博弈基础数据计算出详尽信息
    """
    def __init__(self):
        super(BondReedem, self).__init__()
        self.strategy_name = "强赎博弈策略"
        self.strategy_descrption = "强赎博弈策略"

    def run(self):
        db = DbHandler()
        db.create_session()
        pd_bond_reedem = pd.read_sql_table('JSL_bonds_redeem', con=db.engine)

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = pd_bond_alive[reedem_combine_from_alive_column]

        pd_bond_reedem = pd.merge(pd_bond_reedem, pd_bond_alive, how='left', on='bond_id')

        pd_bond_reedem = pd_bond_reedem[reedem_column + reedem_combine_from_alive_column]

        pd_bond_reedem = pd_bond_reedem.loc[:, ~pd_bond_reedem.columns.duplicated()]

    #
    def api(self, size=10):
        db = DbHandler()
        db.create_session()
        pd_bond_reedem = pd.read_sql_table('JSL_bonds_redeem', con=db.engine)

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = pd_bond_alive[reedem_combine_from_alive_column]

        pd_bond_reedem = pd.merge(pd_bond_reedem, pd_bond_alive, how='left', on='bond_id')

        pd_bond_reedem = pd_bond_reedem[reedem_column + reedem_combine_from_alive_column]

        pd_bond_reedem = pd_bond_reedem.loc[:, ~pd_bond_reedem.columns.duplicated()]


        # 清掉原来的索引
        pd_bond_reedem.reset_index(inplace=True, drop=True)
        pd_bond_reedem = pd_bond_reedem.fillna('NONE')
        db.close_session()
        return pd_bond_reedem

if __name__ == '__main__':
    ins = BondReedem()
    # ins.topN = 20
    ins.run()

